package com.gwtgeo.ui.server.handler;

import org.springframework.beans.factory.annotation.Autowired;

import net.customware.gwt.dispatch.server.ActionHandler;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;

import com.google.inject.Inject;
import com.gwtgeo.service.exception.GwtgeoException;
import com.gwtgeo.service.manager.FileSaveManager;
import com.gwtgeo.ui.server.spring.ActionHandlerBean;
import com.gwtgeo.ui.shared.rpc.SaveDef;
import com.gwtgeo.ui.shared.rpc.SaveDefResult;

@ActionHandlerBean
public class SaveDefHandler implements ActionHandler<SaveDef, SaveDefResult> {

	@Autowired
	private FileSaveManager fileSaveManager;

	@Inject
	public SaveDefHandler() {
		super();
	}

	@Override
	public SaveDefResult execute(SaveDef action, ExecutionContext context)
			throws ActionException {
		try {
			fileSaveManager.save(action.getFileName(), action.getDef());
			return new SaveDefResult();
		} catch (GwtgeoException e) {
			throw new ActionException(e);
		}
	}

	@Override
	public Class<SaveDef> getActionType() {
		return SaveDef.class;
	}

	@Override
	public void rollback(SaveDef arg0, SaveDefResult arg1, ExecutionContext arg2)
			throws ActionException {
		// TODO PMA20100228 Handle failure.
	}

	public void setFileSaveManager(FileSaveManager fileSaveManager) {
		this.fileSaveManager = fileSaveManager;
	}

}
